home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / comm / tcp / Amster.lha / Amster_Install / Source / channellist.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-01  |  6.9 KB  |  232 lines

  1. /*
  2. ** Amster - Channel List
  3. ** by Jacob Laursen <laursen@myself.com>
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/socket.h>
  12. #include <proto/utility.h>
  13.  
  14. #include <netdb.h>
  15. #include <sys/time.h>
  16. #include <sys/socket.h>
  17. #include <sys/ioctl.h>
  18. #include <netinet/tcp.h>
  19. #include <bsdsocket/socketbasetags.h>
  20. #include <error.h>
  21. #include <time.h>
  22.  
  23. #include <MUI/NListview_mcc.h>
  24.  
  25. #include "include/config.h"
  26. #include "include/chat.h"
  27. #include "include/channellist.h"
  28. #include "include/gui.h"
  29. #include "include/info.h"
  30. #include "include/mui.h"
  31. #include "include/prefs.h"
  32.  
  33. #include "include/protos.h"
  34. #include "amster_Cat.h"
  35.  
  36. /* Private prototypes */
  37.  
  38. MUIF ChannelListDisplay(REG(a2) char **array, REG(a1) struct ChannelEntry *entry);
  39. MUIF ChannelListCompare(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm);
  40. MUIF ChannelListDestruct(REG(a2) APTR pool, REG(a1) struct ChannelEntry *entry);
  41.  
  42.  
  43. MUIF ChannelListDispatch(REG(a0) struct IClass *cl, REG(a2) Object *obj, REG(a1) Msg msg)
  44. {
  45.     struct ChannelListData *data;
  46.  
  47.     switch (msg->MethodID) {
  48.         case OM_NEW:
  49.             return(ChannelListNew(cl, obj, (APTR)msg));
  50.         case CHANLIST_UPDATE:
  51.             {
  52.             u_long item;
  53.             data = INST_DATA(cl, obj);
  54.  
  55.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_Visual);
  56.             while (1) {
  57.                 DoMethod(data->LV_Channel, MUIM_NList_GetEntry, 0, &item);
  58.                 if (!item) break;
  59.                 DoMethod(data->LV_Channel, MUIM_NList_Remove, MUIV_NList_Remove_First);
  60.             }
  61.             set(data->LV_Channel, MUIA_NList_Quiet, MUIV_NList_Quiet_None);
  62.             nap_sendbuf(NAPC_LIST_CHANNELS, "");
  63.             break;
  64.             }
  65.         case CHANLIST_JOIN:
  66.             {
  67.             struct ChannelEntry *entry;
  68.             Object *win;
  69.             data = INST_DATA(cl, obj);
  70.  
  71.             DoMethod(data->LV_Channel, MUIM_NList_GetEntry, MUIV_NList_GetEntry_Active, &entry);
  72.             if (entry) {
  73.                 win = NewObject(gui->chat_mcc->mcc_Class, NULL, TAG_DONE);
  74.                 if (!win) return NULL;
  75.                 DoMethod(gui->app, OM_ADDMEMBER, win);
  76.                 set(win, MUIA_Window_Open, TRUE);
  77.                 DoMethod(win, CHAT_JOINCHANNEL, entry->Name);
  78.             }
  79.             return NULL;
  80.             }
  81.         case CHANLIST_ENTRY:
  82.             {
  83.             struct ChannelEntry *entry;
  84.             data = INST_DATA(cl, obj);
  85.  
  86.             if (entry = malloc(sizeof(struct ChannelEntry))) {
  87.                 entry->Name = strdup((char *)(((muimsg)msg)->arg1));
  88.                 entry->NumUsers = (int)(((muimsg)msg)->arg2);
  89.                 entry->Topic = strdup((char *)(((muimsg)msg)->arg3));
  90.                 DoMethod(data->LV_Channel, MUIM_NList_InsertSingle, entry, MUIV_NList_Insert_Sorted);
  91.             }
  92.             break;
  93.             }
  94.     }
  95.     return(DoSuperMethodA(cl, obj, msg));
  96. }
  97.  
  98.  
  99. ULONG ChannelListNew(struct IClass *cl, Object *obj, struct opSet *msg)
  100. {
  101.     static const struct Hook ChannelListDispHook = { {NULL, NULL}, &ChannelListDisplay,  NULL, NULL };
  102.     static const struct Hook ChannelListCompHook = { {NULL, NULL}, &ChannelListCompare,  NULL, NULL };
  103.     static const struct Hook ChannelListDestHook = { {NULL, NULL}, &ChannelListDestruct, NULL, NULL };
  104.  
  105.     struct ChannelListData *data;
  106.  
  107.     Object *LV_Channel;
  108.     Object *BT_Update, *BT_Join;
  109.  
  110.     if (obj = (Object *)DoSuperNew(cl, obj,
  111.         MUIA_HelpNode, "channellist",
  112.         MUIA_Window_Title, MSG_CHANNELLIST_TITLE,
  113.         MUIA_Window_ID, MAKE_ID('C','H','L','S'),
  114.         WindowContents, VGroup,
  115.             Child, LV_Channel = NListviewObject,
  116.                 MUIA_NList_Input, TRUE,
  117.                 MUIA_NListview_NList, NListObject,
  118.                     InputListFrame,
  119.                     MUIA_NList_ListBackground, MUII_ListBack,
  120.                     MUIA_NList_Title, TRUE,
  121.                     MUIA_NList_Format, "BAR,BAR,",
  122.                     MUIA_NList_DisplayHook, &ChannelListDispHook,
  123.                     MUIA_NList_CompareHook2, &ChannelListCompHook,
  124.                     MUIA_NList_DestructHook, &ChannelListDestHook,
  125.                 End,
  126.             End,
  127.             Child, HGroup,
  128.                 Child, BT_Join   = SimpleButton(MSG_CHANNELLIST_JOIN_GAD),
  129.                 Child, BT_Update = SimpleButton(MSG_CHANNELLIST_UPDATE_GAD),
  130.             End,
  131.         End,
  132.         TAG_MORE, msg->ops_AttrList))
  133.     {
  134.         data = INST_DATA(cl, obj);
  135.         data->LV_Channel  = LV_Channel;
  136.  
  137.         DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  138.  
  139.         DoMethod(BT_Update, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_UPDATE);
  140.         DoMethod(BT_Join,   MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CHANLIST_JOIN);
  141.  
  142.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick,  MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_Both);
  143.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_TitleClick2, MUIV_EveryTime, LV_Channel, 4, MUIM_NList_Sort3, MUIV_TriggerValue, MUIV_NList_SortTypeAdd_2Values, MUIV_NList_Sort3_SortType_2);
  144.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType,    MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark,  MUIV_TriggerValue);
  145.         DoMethod(LV_Channel, MUIM_Notify, MUIA_NList_SortType2,   MUIV_EveryTime, LV_Channel, 3, MUIM_Set, MUIA_NList_TitleMark2, MUIV_TriggerValue);
  146.  
  147.         return((ULONG)obj);
  148.     }
  149.     return NULL;
  150. }
  151.  
  152.  
  153. MUIF ChannelListDisplay(REG(a2) char **array, REG(a1) struct ChannelEntry *entry)
  154. {
  155.     static char users[5];
  156.  
  157.     if (entry) {
  158.         *array++ = entry->Name;
  159.         sprintf(users, "%d", entry->NumUsers);
  160.         *array++ = users;
  161.         *array = entry->Topic;
  162.     }
  163.     else {
  164.         *array++ = (char *)MSG_CHANNELLIST_NAME;
  165.         *array++ = (char *)MSG_CHANNELLIST_USERS;
  166.         *array   = (char *)MSG_CHANNELLIST_TOPIC;
  167.     }
  168.  
  169.     return NULL;
  170. }
  171.  
  172.  
  173. MUIF ChannelListCompare(REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct NList_CompareMessage *ncm)
  174. {
  175.     struct ChannelEntry *entry1 = ncm->entry1;
  176.     struct ChannelEntry *entry2 = ncm->entry2;
  177.     LONG col1 = ncm->sort_type & MUIV_NList_TitleMark_ColMask;
  178.     LONG col2 = ncm->sort_type2 & MUIV_NList_TitleMark2_ColMask;
  179.     ULONG result = 0;
  180.  
  181.     if (ncm->sort_type == MUIV_NList_SortType_None) return (0);
  182.  
  183.     if (col1 == 0) {
  184.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  185.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  186.         else
  187.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  188.     }
  189.     else if (col1 == 1) {
  190.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  191.             result = entry2->NumUsers - entry1->NumUsers;
  192.         else
  193.             result = entry1->NumUsers - entry2->NumUsers;
  194.     }
  195.     else if (col1 == 2) {
  196.         if (ncm->sort_type & MUIV_NList_TitleMark_TypeMask)
  197.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  198.         else
  199.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  200.     }
  201.  
  202.     if ((result != 0) || (col1 == col2)) return (result);
  203.  
  204.     if (col2 == 0) {
  205.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  206.             result = (LONG) stricmp(entry2->Name, entry1->Name);
  207.         else
  208.             result = (LONG) stricmp(entry1->Name, entry2->Name);
  209.     }
  210.     else if (col2 == 1) {
  211.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  212.             result = entry2->NumUsers - entry1->NumUsers;
  213.         else
  214.             result = entry1->NumUsers - entry2->NumUsers;
  215.     }
  216.     else if (col2 == 2) {
  217.         if (ncm->sort_type & MUIV_NList_TitleMark2_TypeMask)
  218.             result = (LONG) stricmp(entry2->Topic, entry1->Topic);
  219.         else
  220.             result = (LONG) stricmp(entry1->Topic, entry2->Topic);
  221.     }
  222.  
  223.     return (result);
  224. }
  225.  
  226.  
  227. MUIF ChannelListDestruct(REG(a2) APTR pool, REG(a1) struct ChannelEntry *entry)
  228. {
  229.     free(entry);
  230.     return NULL;
  231. }
  232.